package com.ibm.dao;

import com.ibm.entity.Order;
import com.ibm.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDAO {

    public  List<Order> SelectOrders()
    {
        List<Order> orderList=new ArrayList<>();
        try{
            String sql="select order_id orderId,user_id userId,total_amount totalAmount, user_addr userAddr, product_id productId , order_time orderTime from orders";
            QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
            orderList= queryRunner.query(sql, new BeanListHandler<Order>(Order.class));
        }catch(SQLException e)
        {
            e.printStackTrace();
        }
        return orderList;
    }
    public int deleteOrder(int orderId)
    {
        int i=0;
        try{
            String sql="delete from orders where order_id=?";
            QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());
            i=queryRunner.update(sql, orderId);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return i;
    }


}
